创建 Node.js 应用(基础)

1.引入 required 模块

我们使用 require 指令来载入 http 模块,并将实例化的 HTTP 赋值给变量 http,实例如下:

const http=require('http');

创建服务器

使用 http.createServer() 方法创建服务器,并使用 listen 方法绑定 80 端口。 函数通过 request, response 参数来接收和响应数据。
在项目的根目录下创建一个叫 app.js 的文件,并写入以下代码:

1
2
3
4
5
6
const http=require('http');
http.createServer((request,response)=>{
response.writeHead(200,{"Content-Type":"text/plain;charset=UTF-8"});
response.write('呵呵');
response.end();
}).listen(80);

3.使用 node 命令执行以上的代码:

node app.js

接下来浏览器访问 location:80 就会有 呵呵

请我吃辣条吧~~
-------------本文结束感谢您的阅读-------------